package com.example.ui.share;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.eton.czh.GifView;
import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.AnalyzingJson;
import com.example.net.NetRequest;
import com.example.ui.learn.CurriculumCenterActivity;
import com.example.ui.learn.ListeningActivity;
import com.example.ui.learn.ListeningInfo;

import com.example.ui.learn.util.CurriculumCenter2;
import com.example.ui.learn.util.MyListViewAdapter2;

import com.example.util.AsyncLoadImage;
import com.example.util.RefreshableListView;
import com.example.util.RefreshableListView.OnRefreshListener;

public class HsTalkActivity extends Activity {
	private Button titleLeftBtn,titleRightBtn;
	private TextView titleText;
	private RefreshableListView mListView;
	private String  typeId,thumb,name,title,blId;
	private Intent myIntent;
	private TextView getIntentText;
	private String strTitle,id,data;
	private ArrayList<CurriculumCenter2>  cca_cc;	
	private MyListViewAdapter2 cca_adapter;
	AlertDialog infoad;
	AlertDialog.Builder bulider;
	private GifView mygifView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {		
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.a_cca);
		cca_adapter.type=2;
		myIntent=getIntent();
		strTitle = myIntent.getStringExtra("title");
		id=myIntent.getStringExtra("id");				
		init();
	}
	private void init() {
		bulider = new AlertDialog.Builder(HsTalkActivity.this);
		bulider.setView(View.inflate(HsTalkActivity.this, R.layout.onloading, null))
				.create();
		infoad = bulider.show();
		titleLeftBtn = (Button)findViewById(R.id.layout_title_left_btn);
		titleRightBtn= (Button)findViewById(R.id.layout_title_right_btn);
		titleText=(TextView)findViewById(R.id.layout_title_middle_text);
		getIntentText=(TextView)findViewById(R.id.item01_text_title);
		mListView = (RefreshableListView) findViewById(R.id.item01_listview);
		titleLeftBtn.setText("返回");
		titleText.setText("华商访谈");
		titleRightBtn.setText("正在听");
		Resources r = this.getResources(); 
		int btn_h = (int) r.getDimension(R.dimen.ting_left);
		int btn_left=(int)r.getDimension(R.dimen.text_left);
		titleRightBtn.setPadding(btn_left, 0,  btn_h, 0);
		mygifView = (GifView)findViewById(R.id.testView);
		mygifView.setGifImage(R.drawable.ting);
		mygifView.setVisibility(0);
		getIntentText.setText(strTitle);
		
		cca_cc=new ArrayList<CurriculumCenter2>();
		cca_adapter=new MyListViewAdapter2(this, cca_cc,new AsyncLoadImage());
		mListView.setAdapter(cca_adapter);
		if(HSApplication.item4_cca_cc.size()>0&&HSApplication.item4_cca_cc.get(id)!=null&&HSApplication.item4_cca_cc.get(id).size()>0){
			mListView.setAdapter(cca_adapter);
			cca_cc=HSApplication.item4_cca_cc.get(id);
			h.sendEmptyMessage(1);
		}else{
			new Thread(item2_run).start(); 
		}
		titleLeftBtn.setOnClickListener(cca_ocl);
		titleRightBtn.setOnClickListener(cca_ocl);
		mListView.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh(RefreshableListView listView) {
				new NewDataTask().execute();

			}
		});
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int Position,
					long arg3) {
				
				 typeId=cca_cc.get(Position).getId();
				 thumb=cca_cc.get(Position).getThumb();
				 name=cca_cc.get(Position).getTitle1();
				 title=cca_cc.get(Position).getTitle();
				 blId=cca_cc.get(Position).getBlId();
				 cca_adapter.current=Position;
				cca_adapter.notifyDataSetChanged();
				 final Button btnfalse=(Button) v.findViewById(R.id.aci_btnfalse);		  
				  LinearLayout l = (LinearLayout) v.findViewById(R.id.cca_l2);
				  switch (l.getVisibility()) {
					case View.VISIBLE:
						btnfalse.setVisibility(View.GONE);
						l.setVisibility(View.GONE);
						break;
					case View.GONE:
						TextView kecheng=(TextView)l.findViewById(R.id.kecheng);
						kecheng.setText("内容介绍");
						TextView jiabing=(TextView)l.findViewById(R.id.jiabing);
						jiabing.setText("嘉宾介绍");
						btnfalse.setVisibility(View.VISIBLE);
						l.setVisibility(View.VISIBLE);
					default:  
						break;
					}
				  btnfalse.setOnClickListener(new OnClickListener() {					
					private ListeningInfo listenInfo;
					@Override
					public void onClick(View v) {
						Intent i1_l = new Intent();
						i1_l.setClass(HsTalkActivity.this, ListeningActivity.class);
						if(HSApplication.HSlistInfo.getListen_uri()!=null&&!"".equals(HSApplication.HSlistInfo.getListen_uri())){
								if(HSApplication.HSlistInfo.getListen_itemId().equals(typeId)){
									listenInfo=HSApplication.HSlistInfo;
									listenInfo.setListen_tag(2);
									i1_l.putExtra(ListeningActivity.ISLISTENING,true);
								}else{
									listenInfo=new ListeningInfo("",thumb,title,name,blId,2,typeId,"",-1,-1,0);
									i1_l.putExtra(ListeningActivity.ISLISTENING,false);
								};
							}else{
								listenInfo=new ListeningInfo("",thumb,title,name,blId,2,typeId,"",-1,-1,0);
								i1_l.putExtra("listening",false);
							}
							Bundle lisinfo=new Bundle();
							lisinfo.putParcelable("listen",listenInfo);
							i1_l.putExtras(lisinfo);
							startActivity(i1_l);
							
					}
				});
				
			}
		});
	}
	Runnable item2_run = new Runnable(){  
		@Override  
		public void run() {  
			data=NetRequest.Request_hta(id);	   
		    cca_cc=AnalyzingJson.getdata_hsa(data);
		    HSApplication.item4_cca_cc.put(id, cca_cc);
		    h.sendEmptyMessage(1);
		}
	};
	
  private  Handler h=new Handler(){
		 public void handleMessage(android.os.Message msg) {
			 switch (msg.what) {
			case 1:
				infoad.dismiss();
				mListView.setAdapter(cca_adapter);
				 cca_adapter.setCcInfos(cca_cc);
				 cca_adapter.notifyDataSetChanged();
				break;
			case 2:				
			
				break;
			default:
				break;
			}
			
			
		 };
		
	};
  
		  


	private class NewDataTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}

			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			 new Thread(item2_run).start(); 
			mListView.completeRefreshing();
			super.onPostExecute(result);
		}

	}
	private  OnClickListener cca_ocl=new OnClickListener() {
		
		@Override
		public void onClick(View v) {			
			switch (v.getId()) {
			case R.id.layout_title_left_btn:					
			    finish(); 
				break;
			case R.id.layout_title_right_btn:
				if(HSApplication.HSlistInfo.getListen_uri()==null||"".equals(HSApplication.HSlistInfo.getListen_uri())){
					Toast.makeText(HsTalkActivity.this, "目前您还没有收听过任何课程!",Toast.LENGTH_SHORT).show();
					return;
				}
				Intent i1_l = new Intent();
				i1_l.setClass(HsTalkActivity.this, ListeningActivity.class);
				Bundle lisinfo=new Bundle();
				lisinfo.putParcelable(ListeningActivity.LISTEN,HSApplication.HSlistInfo);
				i1_l.putExtras(lisinfo);
				i1_l.putExtra(ListeningActivity.ISLISTENING,true);
				HsTalkActivity.this.startActivity(i1_l);
				break;
			default:
				break;
			}
		}
	};
	
	
}
